package com.example.peibei.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.dingtao.common.bean.WxLogin;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.LoginWxPresenter;
import com.example.peibei.service.presenter.UserSigPresenter;
import com.example.peibei.ui.activity.LoginActivity;
import com.example.peibei.ui.activity.MainActivity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    private static final String APP_ID = "wx975b50f195258214";
    LoginWxPresenter LoginWxPresenter;
    private UserSigPresenter userSigPresenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoginWxPresenter = new LoginWxPresenter(new UserSigCall());
        userSigPresenter = new UserSigPresenter(new WxLoginCall());
        //注册API
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp instanceof SendAuth.Resp) {
            SendAuth.Resp newResp = (SendAuth.Resp) resp;
            //获取微信传回的code
            String code = newResp.code;
            Log.i("answer", "微信code：" + code);
            LoginWxPresenter.reqeust("1", code);
        }
    }

    class UserSigCall implements DataCall<WxLogin> {

        @Override
        public void success(WxLogin data, Object... args) {
            SPUtils spUtils = new SPUtils(WXEntryActivity.this, SpConstant.USER_TOKEN);
            if (data.getCode().equals("0")){
                Log.i("answer",data.getOpenId()+"");
                UIUtils.showToastSafe("首次登录请绑定手机号");
                spUtils.putString(SpConstant.WX_OPEN_ID,data.getOpenId());
            }else{
                spUtils.putString(SpConstant.USER_TOKEN,data.getToken());
                userSigPresenter.reqeust("Bearer " + data.getToken());
            }
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            Log.i("answer",e.getDisplayMessage());
            finish();
        }
    }

    class WxLoginCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            SPUtils spUtils = new SPUtils(WXEntryActivity.this, SpConstant.USER_TOKEN);
            spUtils.putString(SpConstant.USER_SIG, data);
            startActivity(new Intent(WXEntryActivity.this,MainActivity.class));
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

}
